import { Component, Injector, Input, Output, EventEmitter } from '@angular/core';
import { IonicPage, normalizeURL, NavController, ModalController, NavParams, Platform, ActionSheetController } from 'ionic-angular';
import { BasePage } from '../../basePage';
import { StorageService } from './../../../providers/StorageService';
import { UserService } from './../../../providers/UserService';



/**导入外部加载组件 */
import { File } from '@ionic-native/file';
import { Transfer, TransferObject } from '@ionic-native/transfer';
import { FilePath } from '@ionic-native/file-path';
import { Camera } from '@ionic-native/camera';
import { PIC_URL } from "../../../providers/Config";




declare var cordova: any;

/**
 * Generated class for the PersonalinfoPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-personalinfo',
  templateUrl: 'personalinfo.html',
})
export class PersonalinfoPage extends BasePage {
  showImage: string;
  userId: any;

  fileObjList = [];
  userinfo = {};//获取内存中的数据
  userVo = {};//获取后台返回回来的数据
  userApi = { //需要修改的值
    "user_nickname": '',
    "user_id": 0,
    "user_avator": '',
  };
  picture = '';
 

  @Input() imgpath: string = "assets/img/touxiang.png";//头像
  lastImage: string = null;

  constructor(injector: Injector,
    public camera: Camera,
    public transfer: Transfer,
    public file: File,
    public filePath: FilePath,
    public userService: UserService,
    // private modalCtrl: ModalController,
    private actionSheetCtrl: ActionSheetController,

    // public platform: Platform,
    public stc: StorageService) {
    super(injector);
    this.picture = PIC_URL;
    this.userinfo = this.stc.read('userinfo');
    this.userinfo = this.stc.read('userinfo');
    this.userId = this.userinfo['user_id']
  }
  // constructor(private modalCtrl: ModalController,
  //   private alertCtrl: AlertController,
  //   private actionSheetCtrl: ActionSheetController,
  //   public loadingCtrl: LoadingController,
  //   public viewCtrl: ViewController,
  //   public navCtrl: NavController,
  //   public camera: Camera,
  //   public transfer: Transfer,
  //   public file: File,
  //   public filePath: FilePath,
  //   public stc: StorageService,
  //   public platform: Platform,
  //   public toastCtrl: ToastController,
  //   public userService: UserService,e) {
  //   // super(injector);
  //   this.userinfo = this.stc.read('userinfo');
  //   this.userinfo = this.stc.read('userinfo');
  //   this.userId = this.userinfo['user_id']
  // }



  ionViewDidLoad() {
    this.personalInfoAPI();

  }


  /**文本框修改
   * type:文本框类型
   * inputname：文本框的名称
   * placeholder：文本框的字段名
   * fontnum:文本框文字显示的个数
   * inputvalue:需要回填回去的值
   * field:需改的值，必须对应
   * 文本框打开方式
   * InputCLI('text','昵称','nickname',10)
   * this.openTextArea('nihao','姓名',data=>{
      
    });InputtextPage
   */
  InputCLI(type, inputname, placeholder, fontnum, inputvalue, field) {
    this.openInputText(type, inputname, placeholder, fontnum, inputvalue, data => {
      if (data) {
        this.userApi[field] = data;
        this.userVo[field] = data;
      } else {
        return;
      }

    });
  }
  //处理图片的路径为可以上传的路径
  public pathForImage(img) {
    if (img === null) {
      return '';
    } else {
      return normalizeURL(cordova.file.dataDirectory + img);
    }
  }

  gotoHeadface() {
    // this.navCtrl.push('UploadPicPage');
    // this.navCtrl.push("CitySelectPage");
    let modal = this.modalCtrl.create("UploadPicPage");
    modal.onDidDismiss(data => {
      console.log(data);
      console.log("我是传入的参数")
      if (data) {
        // if (data.code == 1) {
        this.lastImage = data[0];
        // }
      } else {

      }
    });
    modal.present();
  }


  /**收获地址管理 */
  managergoodsCLI() {
    let modal = this.modalCtrl.create('ManagergoodsPage');
    modal.onDidDismiss(data => {
      this.personalInfoAPI()
    });
    modal.present();
  }



  /**返回上一个页面 */
  dismissCLI() {
    this.navCtrl.pop();
  }

  /**修改个人资料 */
  async editUserInfoAPI() {
    this.userApi.user_nickname = this.userVo['user_nickname'];
    this.userApi.user_id = this.userinfo['user_id'];
    this.userApi.user_avator = this.lastImage;

    let loader = this.loadingCtrl.create({
      content: "加载中...",
    });
    // let load=super.showLoading( "")
    let nickname = /^(?!_)(?!.*?_$)[a-zA-Z0-9_\u4e00-\u9fa5]{0,10}$/
    // if (nickname.test(this.userApi.user_nickname)) {
    console.log(this.userApi);
    let result = await this.userService.editUserInfoAPI(this.userApi);
    console.log(result);

    loader.dismiss();
    // console.log(result)
    // console.log(this.userApi)
    if (result.code == 1) {
      this.showAlert(result.msg)
      this.navCtrl.pop();
    } else {
      this.showAlert(result.msg);
    }
    // } else {
    //   this.showAlert('昵称不合法')
    // }
  }


  /**获取个人资料的信息 */
  async personalInfoAPI() {
    let param = { 'user_id': this.userinfo['user_id'] };
    let loader = this.loadingCtrl.create({
      content: "",
    });
    loader.present();
    let result = await this.userService.doPersonalInfoAPI(param);
    loader.dismiss();
    if (result.code == 1) {
      this.userVo = result.data;
      console.log(this.userVo);
      this.lastImage = this.userVo['user_avator'];
      // this.createPhotos();
    } else {
      this.showAlert(result.msg);
    }
  }


  /**图片预览 */
  // createPhotos() {

  // }



}
